﻿
var activePanel;
var postTab;
$(function () {
    
    var tabCounter = 0;
    var tabTemplate = '<li><a href="#{href}">#{label}</a><span class="ui-icon ui-icon-close" role="presentation">Remove Tab</span></li>';
    mainTabs = $("#mainTabs").tabs({
        activate: function (event, ui) {
            activePanel = 'tabs-' + ui.newTab.find("a").attr("id").match(/\d+/);
            //alert(activePanel);
        },
        create: function (event, ui) {
            tabCounter = $("#mainTabs li").length + 1;
            activePanel = 'tabs-'+ui.tab.find("a").attr("id").match(/\d+/);
            //alert(activePanel);
        }
       
    });
    mainTabs.find(".ui-tabs-nav").sortable({ axis: "x" })
    //添加TAB
    function addTab(tabTitle, tabContent) {
        var label = tabTitle || "Tab " + tabCounter,
          id = "tabs-" + tabCounter,
          li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label)),
          tabContentHtml = tabContent || "Tab " + tabCounter + " content.";

        mainTabs.find(".ui-tabs-nav").append(li);
        mainTabs.append("<div id='" + id + "'><p>" + tabContentHtml + "</p></div>");
        mainTabs.tabs("refresh");
        tabCounter++;
        

    }
    //绑定关闭TAB事件
    mainTabs.delegate("span.ui-icon-close", "click", function () {
        var panelId = $(this).closest("li").remove().attr("aria-controls");
        $("#" + panelId).remove();
        mainTabs.tabs("refresh");
    });
    $("#add_tab").click(function(){addTab();});
    

        
});

function openUrl() {
    $.get("/Admin/Announce/Form", function (data, textStatus){
        $("#mainTabs #" + activePanel).html(data);
        $.validator.unobtrusive.parse("#mainTabs #" + activePanel);
    })
}

//$("#mainTabs span.ui-icon-close").live("click", function () {
//    removeTab(this);

//});




